package com.itheima.controller;


import com.alibaba.fastjson.JSONObject;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/checkgroup/")
public class CheckGroupController {

    @Reference(timeout = 7000)
    private CheckGroupService checkGroupService;

    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return checkGroupService.pageQuery(queryPageBean);
    }

    @RequestMapping("findAll")
    public Result findPage(){
        try {
            List<CheckGroup> result = checkGroupService.findAll(null).getResult();
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,result);
        } catch (Exception e) {
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("add")
    public Result add(@RequestBody Map<String, Object> map ){
        try {
            CheckGroup checkGroup = JSONObject.parseObject(map.get("formData").toString(),CheckGroup.class);
            Integer[] checkitemIds = JSONObject.parseObject(map.get("checkitemIds").toString(), Integer[].class);
            checkGroupService.add(checkGroup,checkitemIds);
            return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }

    }

    @RequestMapping("findCheckGroupById")
    public Result findCheckGroupById(Integer id){

        /*
        * 返回组数据
        * 返回所有项数据
        * 返回所有绑定项数据
        * */

        try {
            Map<String, Object> checkGroup = checkGroupService.findById(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        } catch (Exception e) {
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
    @RequestMapping("edit")
    public Result edit(@RequestBody Map<String, Object> map){
        try {
            CheckGroup checkGroup = JSONObject.parseObject(map.get("formData").toString(),CheckGroup.class);
            Integer[] checkitemIds = JSONObject.parseObject(map.get("checkitemIds").toString(), Integer[].class);

            checkGroupService.edit(checkGroup,checkitemIds);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }


    @RequestMapping("delete")
    public Result delete(Integer id){
        try {
            checkGroupService.delete(id);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }


}
